/*
 * Copyright (C) 2017 Pingan, Inc. All Rights Reserved.
 */

package com.pingan.haofang.searchcloud.es.schema.impl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.pingan.haofang.searchcloud.api.schema.IndexSchema;
import com.pingan.haofang.searchcloud.es.indices.IndicesDataService;
import com.pingan.haofang.searchcloud.es.schema.MappingManageService;

/**
 * mapping管理
 *
 * @author zhangbi617
 * @date 2017-07-04
 */
@Deprecated
//@Service
public class MappingManageServiceImpl implements MappingManageService {

    /**
     * schema存的索引
     */
    private static final String DEFAULT_SCHEMA_INDEX = ".searchcloud_schema";

    /**
     * type
     */
    private static final String DEFAULT_SCHEMA_TYPE = "searchcloud_schema";

    private IndicesDataService indexDataService;

    private static Gson GSON = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

    public MappingManageServiceImpl(IndicesDataService indexDataService) {
        this.indexDataService = indexDataService;
    }

    @Override
    public void create(String host, IndexSchema schema, String configName) {
        indexDataService.create(host, DEFAULT_SCHEMA_INDEX, DEFAULT_SCHEMA_TYPE, configName, GSON.toJson(schema));
    }

    @Override
    public IndexSchema get(String host, String configName) {
        String source = indexDataService.getData(host, DEFAULT_SCHEMA_INDEX, DEFAULT_SCHEMA_TYPE, configName);
        return GSON.fromJson(source, IndexSchema.class);
    }

    @Override
    public void delete(String host, String configName) {
        indexDataService.deleteData(host, DEFAULT_SCHEMA_INDEX, DEFAULT_SCHEMA_TYPE, configName);
    }
}
